package com.theaf.basemodule.web.controller;

import com.theaf.basemodule.common.api.ResourceService;
import com.theaf.basemodule.common.condition.ResourceCondition;
import com.theaf.basemodule.common.dto.resource.AppDTO;
import com.theaf.basemodule.common.dto.resource.AuthDTO;
import com.theaf.basemodule.common.dto.resource.ResourceDTO;
import com.theaf.basemodule.common.vo.resource.ResourceVO;
import com.theaf.core.web.ResultHandler;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value="/resource")
@Api(value = "/resource",description = "资源请求")
public class ResourceController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private ResourceService resourceService;

    @RequestMapping(value = "/apps",  method = RequestMethod.GET)
    @ApiOperation(value = "系统列表", notes = "系统列表", response = AppDTO.class)
    public Object app(){
        return ResultHandler.success(resourceService.findAllApp());
    }

    @RequestMapping(value = "/auths/{appId}",  method = RequestMethod.GET)
    @ApiOperation(value = "获取系统的模块及功能", notes = "获取系统的模块及功能", response = AuthDTO.class)
    @ApiImplicitParam(value = "系统ID" , name = "appId" , dataType = "long" , paramType = "path",required=true)
    public Object auths(@PathVariable("appId") Long appId){
        return ResultHandler.success(resourceService.findAuths(appId));
    }

    @RequestMapping(value = "/list",  method = RequestMethod.POST)
    @ApiOperation(value = "资源查询", notes = "资源查询", response = ResourceDTO.class)
    public Object list(@RequestBody @ApiParam(name="查询条件",value="传入json格式",required=true) ResourceCondition condition){
        return ResultHandler.success(resourceService.findList(condition));
    }

    @RequestMapping(value = "/get/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "获取资源数据", notes = "获取资源数据", response = ResourceDTO.class)
    @ApiImplicitParam(value = "标识" , name = "id" , dataType = "long" , paramType = "path",required=true)
    public Object get(@PathVariable("id") Long id){
        return ResultHandler.success(resourceService.findById(id));
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "保存资源", httpMethod = "POST" ,response = Boolean.class)
    public Object save(@RequestBody @Validated @ApiParam(name="资源数据",value="传入json格式",required=true) ResourceVO vo){
        return ResultHandler.success(resourceService.save(vo));
    }

    @RequestMapping(value = "/delete/{id}",  method = RequestMethod.DELETE)
    @ApiOperation(value = "删除资源", notes = "删除资源", response = Boolean.class)
    @ApiImplicitParam(value = "资源标识" , name = "id" , dataType = "long" , paramType = "path",required=true)
    public Object delete(@PathVariable("id") Long id){
        resourceService.delete(id);
        return ResultHandler.success(true);
    }
}
